#include "checkpage.h"

#if debug
#include <stdio.h>
#endif

#include "resource.h"


curl_c * curl;

char * fname;
char tmpname[] = "data\\temp.htm";
char dataname[] = "data\\";
bool ok = true;
bool f1ok = true;
bool f2ok;
textfile_c * file1 = new textfile_c;
textfile_c * file2 = new textfile_c;
char * __tmpfile;

int CheckPage(HWND wnd, int page) {

  if (page == 0) {
    pages->disableButtons();
    progressbar->setRange(0,3);
////////////
    progressbar->setValue(0);
    language->setControlText(progresslabel,6);
    curl = new curl_c;
    progressbar->setValue(1);
    SetTimer(wnd,TIMER_STEP1,10000,NULL);
  } else if (page == 1) {
////////////
    fname = new char[strlen(SystemDefault->ExePath)+strlen(tmpname)+1];
    memcpy(fname,SystemDefault->ExePath,strlen(SystemDefault->ExePath));
    memcpy(fname+strlen(SystemDefault->ExePath),tmpname,strlen(tmpname));
    fname[strlen(SystemDefault->ExePath)+strlen(tmpname)] = 0;
    language->setControlText(progresslabel,8);
    printf("2\n");
    curl->getPage("fname",edit->getText());
    printf("3\n");
    if (curl->res != CURLE_OK) {
      char * tmp = new char[512];
      sprintf(tmp,"%s",curl_easy_strerror(curl->res));
      progresslabel->setText(tmp);
      delete tmp;
//      file->CloseFile();
      pages->enableButtons();
    } else {
      progressbar->setValue(2);
//      file->CloseFile();
      SetTimer(wnd,TIMER_STEP2,1000,NULL);
    }
    delete curl;
//    curl_easy_cleanup(xcurl->c);
////////////
  } else if (page == 2) {
    language->setControlText(progresslabel,9);
    if (strlen(data->getFile())== 0) {
      GUID *pguid = new GUID;
      CoCreateGuid(pguid);
      char * GuidStr;//
      UuidToString(pguid, (unsigned char**)&GuidStr);
      char * GUIDName = new char[50];
      int asize = strlen(GuidStr);
      memcpy(GUIDName,GuidStr,asize);
      GUIDName[asize] = '.';
      GUIDName[asize+1] = 'h';
      GUIDName[asize+2] = 't';
      GUIDName[asize+3] = 'm';
      GUIDName[asize+4] = 0;
      delete pguid;
      __tmpfile = new char[strlen(SystemDefault->ExePath)+strlen(dataname)+strlen(GUIDName)+1];
      memcpy(__tmpfile,SystemDefault->ExePath,strlen(SystemDefault->ExePath));
      memcpy(__tmpfile+strlen(SystemDefault->ExePath),dataname,strlen(dataname));
      memcpy(__tmpfile+strlen(SystemDefault->ExePath)+strlen(dataname),GUIDName,strlen(GUIDName));
      __tmpfile[strlen(SystemDefault->ExePath)+strlen(dataname)+strlen(GUIDName)] = 0;
      MoveFileEx(fname,__tmpfile,MOVEFILE_REPLACE_EXISTING);
      data->setFile(GUIDName);
      progressbar->setValue(3);
      pages->enableButtons();
    } else {
      progressbar->setValue(2);
      SetTimer(wnd,TIMER_STEP3,1000,NULL);
    }
  } else if (page == 3) {
    language->setControlText(progresslabel,10);
    __tmpfile = new char[strlen(SystemDefault->ExePath)+strlen(dataname)+strlen(data->getFile())+1];
    memcpy(__tmpfile,SystemDefault->ExePath,strlen(SystemDefault->ExePath));
    memcpy(__tmpfile+strlen(SystemDefault->ExePath),dataname,strlen(dataname));
    memcpy(__tmpfile+strlen(SystemDefault->ExePath)+strlen(dataname),data->getFile(),strlen(data->getFile()));
    __tmpfile[strlen(SystemDefault->ExePath)+strlen(dataname)+strlen(data->getFile())] = 0;
    if (file1->OpenReadTextFile(fname)) {
      if (file2->OpenReadTextFile(__tmpfile)) {
        progressbar->setValue(3);
        SetTimer(wnd,TIMER_STEP4,1000,NULL);
      } else {
        ok = false;                                  // Dateien nicht identisch
      }
    } else {
      ok = false;                                   // Dateien nicht identisch
    }
    if (!ok) {
      delete file2;
      pages->enableButtons();
    }
    if (ok) {printf("ok\n");}
    printf("%s\n%s\n",__tmpfile,fname);
  } else if (page == 4) {
     while (ok & f1ok) {
       f1ok = file1->getChar();
       f2ok = file2->getChar();
       if (f1ok == f2ok)  {
         ok = (file1->currentChar == file2->currentChar);
       } else {
         ok = false;
       }
    }
    file1->CloseFile();
    file2->CloseFile();
    delete file1;
    delete file2;
    pages->enableButtons();
  }
  return 0;
}
